package cc.lj.internal.mobilepay.application.utils;

import org.springframework.core.io.ClassPathResource;
import org.springframework.stereotype.Component;

import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.InputStream;

/**
 * 中信签名用到的文件工具类
 * @author liangxifeng
 * @date 2022/12/20 16:29
 */
@Component
public class FileUtil {
    public static byte[] read4file(String filename) throws Exception {
        ClassPathResource resource = new ClassPathResource(filename);
        InputStream inputStream =  resource.getInputStream();
        FileInputStream fis = null;
        ByteArrayOutputStream baos = null;

        try {
            //fis = new FileInputStream(filename);
            baos = new ByteArrayOutputStream();
            byte[] bytes = new byte[128];
            boolean var4 = false;

            int n;
            while((n = inputStream.read(bytes)) > 0) {
                baos.write(bytes, 0, n);
            }
        } finally {
            try {
                if (fis != null) {
                    fis.close();
                }

                if (baos != null) {
                    baos.close();
                }
            } catch (Exception var10) {
            }

        }

        return baos.toByteArray();
    }
}
